<?php

namespace App\Console\Commands;

use App\Library\Logic\PaymentRelatedLogic;
use App\Models\PaymentModel;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class PaymentRelatedCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:paymentRelated';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // 设置时间界限，20秒之前的订单
        $timestamp = Carbon::now()->subSeconds(25)->format('Y-m-d H:i:s');
        $relInfo = PaymentRelatedLogic::query()->where([
            'status_pay' => 0,
            'status_refund' => 0,
            ['created_at', '<', $timestamp]
        ])->pluck('id')->toArray();

        if (!empty($relInfo)) {
            PaymentRelatedLogic::query()
                ->whereIn('id', $relInfo)->delete();
            PaymentModel::query()->where([
                'status_refund' => 0,
                ['pay_at', '=', null],
                ['refund', '=', null]
            ])->whereIn('payment_related_id', $relInfo)->delete();
        }
    }
}
